package com.webinsky.article.persist.dao;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ArticleTagMapperProvider {
	
	private static final String SELECT_TEMPLATE = "select id, article_id, tag_id, del, create_at, update_at, language from `article_tag`";
	
	@SuppressWarnings("unchecked")
	public String selectActiveByTagIds(Map<String, Object> params) {
		List<Integer> tagIds = (List<Integer>)params.get("tagIds");
		String sql = SELECT_TEMPLATE + " where del = 0 ";
		if ( tagIds != null && !tagIds.isEmpty() ) {
			List<String> strTagIds = tagIds.stream().map(e -> e.toString()).collect(Collectors.toList());
			String condition = "tag_id in (" + String.join(",", strTagIds) + ")";
			return sql + " and " + condition;
		}
		return sql;
	}
	
	@SuppressWarnings("unchecked")
	public String selectByActiveByArticleIds(Map<String, Object> params){
		List<Integer> articleIds = (List<Integer>)params.get("articleIds");
		String sql = SELECT_TEMPLATE + " where del = 0 ";
		if ( articleIds != null && !articleIds.isEmpty() ){
			List<String> strArticleIds = articleIds.stream().map(e -> e.toString()).collect(Collectors.toList());
			String condition = "article_id in (" + String.join(",", strArticleIds) + ")";
			return sql + " and " + condition;
		}
		return sql;
	}
}
